﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XDYRestaurant
{
    public partial class Main : Form
    {
        private Context.Context ctx = new Context.Context();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
        private static int index;
        public Main()
        {
            InitializeComponent();
        }
        private void LoadTableInfo(object sender, EventArgs e)
        {
            //当进行餐桌信息修改时,清除所有信息 
            listView1.Clear();
            this.imageList1.Images.Clear();
            var list = ctx.Tables.Where(s => true).ToList();
            foreach (var item in list)
            {
                if(item.TableStatic == 0)
                {
                    this.imageList1.Images.Add(item.TableId.ToString(), (System.Drawing.Image)(resources.GetObject("desk1.Image")));
                }
                else
                {
                    this.imageList1.Images.Add(item.TableId.ToString(), (System.Drawing.Image)(resources.GetObject("desk2.Image")));
                }
            }
            listView1.LargeImageList = imageList1;
            listView1.DoubleClick += LvTableInfo_DoubleClick;
            for (int i = 0; i < imageList1.Images.Count; i++)
            {
                var lvi = new ListViewItem();
                lvi.ImageIndex = i;
                lvi.Text = list[i].TableId.ToString();
                lvi.ToolTipText = list[i].TableId.ToString();
                listView1.Items.Add(lvi);
            }
        }
        private void menuMemberInfo_Click(object sender, EventArgs e)
        {

            CustomerInfo customerInfo = CustomerInfo.Create();
            customerInfo.Show();
        }

        //菜品管理
        private void menuDishInfo_Click(object sender, EventArgs e)
        {
            DishInfo dishInfo = DishInfo.Create(); 
            dishInfo.Show();
        }
        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuQuit_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确认要退出程序吗?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK) Application.Exit();
        }
        private void menuTableInfo_Click(object sender, EventArgs e)
        {
            TableInfo tableInfo = TableInfo.Create();
            tableInfo.Show();
        }
        private void menugetNewWinform_Click(object sender, EventArgs e)
        {
            this.Hide(); //先隐藏主窗体
            Main form1 = new Main(); //重新实例化此窗体
            form1.ShowDialog();//已模式窗体的方法重新打开
            this.Close();//原窗体关闭
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)                   
            {
                index = int.Parse(listView1.SelectedItems[0].Text);
            }
        }
        private void LvTableInfo_DoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show("访问"+index.ToString()+"号桌");
            if (ctx.Tables.FirstOrDefault(s => s.TableId == index).TableStatic == 0)
            {
                MessageBox.Show("当前餐桌没有顾客", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            OrderInfo orderInfo = OrderInfo.Create(index);
            orderInfo.ShowDialog();

        }

        private void menuOrder_Click(object sender, EventArgs e)
        {
            MessageBox.Show("结算" + index.ToString() + "号桌");
            if (ctx.Tables.FirstOrDefault(s => s.TableId == index).TableStatic == 0)
            {
                MessageBox.Show("当前餐桌没有顾客", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            Payment payment = Payment.Create(index);
            payment.Show();
        }
    }
}
